/**
 * 删除角色
 * @url admin/system/role/sys/delete
 * @description 删除角色
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '删除成功' };
		
		// 业务逻辑开始-----------------------------------------------------------
		let { _id, ids } = data;
		
		if (!_id && !ids) {
			return { code: -1, msg: '请指定要删除的角色' };
		}
		
		let deleteIds = ids || [_id];
		
		// 检查是否有用户使用该角色
		let userRes = await db.collection('uni-id-users')
			.where({
				role: _.in(deleteIds)
			})
			.get();
		
		if (userRes.data.length > 0) {
			return { code: -1, msg: '该角色正在被使用，无法删除' };
		}
		
		// 删除角色
		await db.collection('uni-id-roles')
			.where({ _id: _.in(deleteIds) })
			.remove();
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

